Skip to content

Commit

Permalink
refactor(core)!: Subject methods will return self instead of void.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Jan 29, 2022
1 parent e3118b3 commit bd17db3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/Observer/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@ public function __construct() {
$this->reset();
}

/**
* @param Closure(TContext):void $observer
*/
public function attach(Closure $observer): void {
public function attach(Closure $observer): static {
$this->observers->attach($observer);

return $this;
}

/**
* @param Closure(TContext):void $observer
*/
public function detach(Closure $observer): void {
public function detach(Closure $observer): static {
$this->observers->detach($observer);

return $this;
}

public function reset(): void {
public function reset(): static {
$this->observers = new SplObjectStorage();

return $this;
}

/**
* @param TContext $context
*
* @return $this<TContext>
*/
public function notify(mixed $context = null): void {
public function notify(mixed $context = null): static {
foreach ($this->observers as $observer) {
/** @var Closure $observer */
$observer($context);
}

return $this;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Observer/Subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
interface Subject {
/**
* @param Closure(TContext):void $observer
*
* @return $this<TContext>
*/
public function attach(Closure $observer): void;
public function attach(Closure $observer): self;

/**
* @param Closure(TContext):void $observer
*
* @return $this<TContext>
*/
public function detach(Closure $observer): void;
public function detach(Closure $observer): self;

/**
* @return $this<TContext>
*/
public function reset(): self;
}

0 comments on commit bd17db3

Please sign in to comment.