Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActorRef #1163

Merged
merged 73 commits into from
May 21, 2020
Merged
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
b18c710
Revert state.children to object instead of array
Apr 9, 2020
5c3e82c
Update snapshot tests
Apr 9, 2020
8c85e8b
WIP incorporating actorref classes
Apr 10, 2020
42bbea4
Merge branch 'master' into v5/actorref
Apr 12, 2020
559398b
Merge branch 'next' into v5/actorref
Apr 12, 2020
477060c
Fixing tests (WIP)
Apr 12, 2020
10a8819
Make Interpreter implement ActorRef
Apr 12, 2020
34f958e
Add start() to ActorRef interface
Apr 19, 2020
9482731
Remove spawn (WIP)
Apr 19, 2020
091e3c1
WIP: spawn without spawn()
Apr 19, 2020
7763a59
Clean-up WIP
Apr 20, 2020
73898d1
Went wrong direction; ids should be explicitly named
Apr 21, 2020
0a8bafa
WIP
Apr 24, 2020
705f12b
Remove idNameMap, fix tests
Apr 24, 2020
20f5bc4
Add behaviors, remove global spawn!
Apr 25, 2020
2a4dde5
More behavior spawning
Apr 26, 2020
4e9ea1f
Refactor ActorRef creators
davidkpiano Apr 28, 2020
20e08e3
Remove createInvocableActor
davidkpiano Apr 28, 2020
21e4ed7
Remove original Actor interface
davidkpiano Apr 28, 2020
654f550
Remove actions.start, add actions.invoke, change actions.stop, remove…
davidkpiano Apr 29, 2020
e196ebb
Fix types
davidkpiano Apr 29, 2020
713e247
Cleaning up activity references
davidkpiano Apr 29, 2020
8c521ff
Simplify ActorRef type, change spawn* to invoke*
davidkpiano Apr 29, 2020
310d77c
Remove interpreter.initialized property, remove spawnMachine method
davidkpiano Apr 29, 2020
c7c70a1
Fixing types WIP
davidkpiano Apr 29, 2020
a23717c
Add behavior package, add spawn.from, add createBehaviorFrom, fix (mo…
davidkpiano May 3, 2020
2fd378f
Add ActorRefFrom type, include initial emitted value in behavior
davidkpiano May 4, 2020
fdeb773
Fixing types
davidkpiano May 6, 2020
9925bce
Typedoc
davidkpiano May 6, 2020
da6b28f
Rename: BehaviorActorRef -> ObservableActorRef
davidkpiano May 8, 2020
79fb79b
Merge branch 'next' into v5/actorref
davidkpiano May 9, 2020
32aa2de
Fix tests
davidkpiano May 9, 2020
8d8daf7
Revert interpreter.current -> interpreter.state & fix types
davidkpiano May 9, 2020
2af052e
Update index.md
FredyC May 9, 2020
bafa9c1
Merge pull request #1181 from FredyC/patch-3
davidkpiano May 9, 2020
55862c0
Move and export ActorRef and ActorRefFrom types
davidkpiano May 9, 2020
0958fda
Fix ActorRef type references
davidkpiano May 9, 2020
f24a8e0
Enforce subscription
davidkpiano May 9, 2020
eebcf42
Refactor ObservableActorRef (behavior private, id -> name)
davidkpiano May 9, 2020
371ad3f
Change initial -> current
davidkpiano May 9, 2020
b331613
Remove ref
davidkpiano May 9, 2020
b97997f
Remove interpreter.spawn
davidkpiano May 9, 2020
1316980
Remove comment
davidkpiano May 9, 2020
c1bba85
Refactor isActorRef
davidkpiano May 9, 2020
b714988
Remove spies
davidkpiano May 9, 2020
499d68b
Symbol -> Symbol.for
davidkpiano May 9, 2020
62f49ca
Fix types
davidkpiano May 9, 2020
985052a
Fix build
davidkpiano May 9, 2020
086b63b
Fix build (again)
davidkpiano May 9, 2020
13918f4
Add simple mailbox
davidkpiano May 10, 2020
194d2b9
doesn't exist -> already exists (for clarity)
djD-REK May 10, 2020
84dbd2e
Merge pull request #1182 from djD-REK/patch-1
davidkpiano May 10, 2020
f685e67
Update README.md
davidkpiano May 10, 2020
5869c88
Ensure actions are executed from a restored initial state. Fixes #1174
davidkpiano May 10, 2020
dbc6a16
Add changeset
davidkpiano May 10, 2020
5b0c2f4
Fix wording on test
davidkpiano May 10, 2020
b967db6
Merge pull request #1183 from davidkpiano/davidkpiano/1174
davidkpiano May 10, 2020
9ddcd94
Separate Actor and ActorRef
davidkpiano May 12, 2020
326db72
Fixed an issue with invoked service not being correctly started
Andarist May 13, 2020
560e28f
Merge pull request #1185 from davidkpiano/fix/1180
davidkpiano May 13, 2020
942ef24
Merge branch 'master' into next
davidkpiano May 16, 2020
0de2d4d
Address failling tests (temporarily)
davidkpiano May 16, 2020
e0163c0
Merge branch 'next' into v5/actorref
davidkpiano May 16, 2020
7713bba
Rename services -> behaviors
davidkpiano May 16, 2020
13a272c
Fix observer next null check
davidkpiano May 16, 2020
1a0e07d
Remove observer.complete being called after observer.error
davidkpiano May 16, 2020
c67923f
Add comment for getInitialState(self)
davidkpiano May 16, 2020
468ff9b
Move setting processingStatus outside of loop
davidkpiano May 16, 2020
85c938d
Remove ActionTypes.Start, use ActionTypes.Invoke
davidkpiano May 16, 2020
4260d6c
More services -> behaviors renaming
davidkpiano May 16, 2020
5569d75
Fix services -> behaviors in UseMachine.vue test
davidkpiano May 16, 2020
390eaaa
Add changeset + clean up invoke.test.ts
davidkpiano May 21, 2020
374d9f1
Modify changeset
davidkpiano May 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add start() to ActorRef interface
David Khourshid committed Apr 19, 2020
commit 34f958ea7762348a2484651cdc8ba5210156b21e
65 changes: 41 additions & 24 deletions packages/core/src/Actor.ts
Original file line number Diff line number Diff line change
@@ -74,7 +74,9 @@ export interface ActorRef<TCurrent, TEvent extends EventObject, TRef = any> {
// subscribe: Subscribable<TCurrent>['subscribe'];
current: TCurrent;
ref: TRef;
start: () => void;
stop: () => void;
id: string;
// subscription?: Unsubscribable;
}

@@ -85,18 +87,24 @@ class ObservableActorRef<TCurrent extends EventObject>

constructor(
public ref: Subscribable<TCurrent>,
parent: ActorRef<any, any>,
id: string // TODO: use "system"
) {
this.subscription = ref.subscribe(
private parent: ActorRef<any, any>,
public id: string // TODO: use "system"
) {}

public start() {
this.subscription = this.ref.subscribe(
(value) => {
parent.send(toSCXMLEvent(value, { origin: this }));
this.parent.send(toSCXMLEvent(value, { origin: this }));
},
(err) => {
parent.send(toSCXMLEvent(error(id, err) as any, { origin: this }));
this.parent.send(
toSCXMLEvent(error(this.id, err) as any, { origin: this })
);
},
() => {
parent.send(toSCXMLEvent(doneInvoke(id) as any, { origin: this }));
this.parent.send(
toSCXMLEvent(doneInvoke(this.id) as any, { origin: this })
);
}
);
}
@@ -124,21 +132,26 @@ class PromiseActorRef<T> implements ActorRef<T | undefined, never, Promise<T>> {
private canceled = false;
public current: T | undefined = undefined;

constructor(public ref: Promise<T>, parent: ActorRef<any, any>, id: string) {
constructor(
public ref: Promise<T>,
private parent: ActorRef<any, any>,
public id: string
) {}
public start() {
this.ref.then(
(response) => {
if (!this.canceled) {
this.current = response;
parent.send(
toSCXMLEvent(doneInvoke(id, response) as any, { origin: this })
this.parent.send(
toSCXMLEvent(doneInvoke(this.id, response) as any, { origin: this })
);
}
},
(errorData) => {
if (!this.canceled) {
const errorEvent = error(id, errorData);
const errorEvent = error(this.id, errorData);

parent.send(toSCXMLEvent(errorEvent, { origin: this }));
this.parent.send(toSCXMLEvent(errorEvent, { origin: this }));
}
}
);
@@ -171,17 +184,18 @@ class CallbackActorRef<

constructor(
public ref: InvokeCallback,
parent: ActorRef<any, any>,
id: string
) {
private parent: ActorRef<any, any>,
public id: string
) {}
public start() {
const dispose = this.ref(
(e: TEmitted) => {
if (this.canceled) {
return;
}

this.current = e;
parent.send(toSCXMLEvent(e, { origin: this }));
this.parent.send(toSCXMLEvent(e, { origin: this }));
},
(newListener) => {
this.receivers.add(newListener);
@@ -191,14 +205,14 @@ class CallbackActorRef<
if (isPromiseLike(dispose)) {
dispose.then(
(resolved) => {
parent.send(
toSCXMLEvent(doneInvoke(id, resolved) as any, { origin: this })
this.parent.send(
toSCXMLEvent(doneInvoke(this.id, resolved) as any, { origin: this })
);
this.canceled = true;
},
(errorData) => {
const errorEvent = error(id, errorData);
parent.send(toSCXMLEvent(errorEvent, { origin: this }));
const errorEvent = error(this.id, errorData);
this.parent.send(toSCXMLEvent(errorEvent, { origin: this }));
// TODO: handle error
this.canceled = true;
}
@@ -269,10 +283,12 @@ class ServiceActorRef<TContext, TEvent extends EventObject>
constructor(
public ref: ActorLike<State<TContext, TEvent>, TEvent> & {
state: State<TContext, TEvent>;
}
},
public id: string
) {
this.current = this.ref.state;

}
public start() {
this.subscription = this.ref.subscribe((state) => {
this.current = state;
});
@@ -288,9 +304,10 @@ class ServiceActorRef<TContext, TEvent extends EventObject>
export function fromService<TContext, TEvent extends EventObject>(
service: ActorLike<State<TContext, TEvent>, TEvent> & {
state: State<TContext, TEvent>;
}
},
id: string
): ActorRef<State<TContext, TEvent>, TEvent, typeof service> {
return new ServiceActorRef<TContext, TEvent>(service);
return new ServiceActorRef<TContext, TEvent>(service, id);
// return new ActorRef(
// service.send.bind(service),
// service.subscribe.bind(service),
24 changes: 16 additions & 8 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -841,9 +841,8 @@ export class Interpreter<

// Invoked services
if (activity.type === ActionTypes.Invoke) {
const serviceCreator:
| ServiceConfig<TContext, TEvent>
| undefined = this.machine.options.services
const actorCreator: ServiceConfig<TContext, TEvent> | undefined = this
.machine.options.services
? this.machine.options.services[activity.src]
: undefined;

@@ -854,7 +853,7 @@ export class Interpreter<
? activity.autoForward
: !!activity.forward;

if (!serviceCreator) {
if (!actorCreator) {
// tslint:disable-next-line:no-console
if (!IS_PRODUCTION) {
warn(
@@ -866,13 +865,16 @@ export class Interpreter<
}

try {
const actor = serviceCreator(context, _event.data, {
const actor = actorCreator(context, _event.data, {
parent: this as any,
id,
data,
_event
});

// start the actor
actor.start();

if (autoForward) {
this.forwardTo.add(id);
}
@@ -1134,11 +1136,11 @@ export function spawn<TC, TE extends EventObject>(
export function spawn(
entity: Spawnable,
nameOrOptions?: string | SpawnOptions
): Actor;
): ActorRef<any, any>;
export function spawn(
entity: Spawnable,
nameOrOptions?: string | SpawnOptions
): Actor {
): ActorRef<any, any> {
const resolvedOptions = resolveSpawnOptions(nameOrOptions);

return withServiceScope(undefined, (service) => {
@@ -1152,7 +1154,13 @@ export function spawn(
}

if (service) {
return service.spawn(entity, resolvedOptions.name, resolvedOptions);
const spawned = service.spawn(
entity,
resolvedOptions.name,
resolvedOptions
);
spawned.start();
return spawned;
} else {
return createNullActor(resolvedOptions.name);
}