Actor.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { __assign } from './_virtual/_tslib.js';
  2. import { symbolObservable, toInvokeSource, mapContext, isMachine } from './utils.js';
  3. import { provide } from './serviceScope.js';
  4. function createNullActor(id) {
  5. var _a;
  6. return _a = {
  7. id: id,
  8. send: function () {
  9. return void 0;
  10. },
  11. subscribe: function () {
  12. return {
  13. unsubscribe: function () {
  14. return void 0;
  15. }
  16. };
  17. },
  18. getSnapshot: function () {
  19. return undefined;
  20. },
  21. toJSON: function () {
  22. return {
  23. id: id
  24. };
  25. }
  26. }, _a[symbolObservable] = function () {
  27. return this;
  28. }, _a;
  29. }
  30. /**
  31. * Creates a deferred actor that is able to be invoked given the provided
  32. * invocation information in its `.meta` value.
  33. *
  34. * @param invokeDefinition The meta information needed to invoke the actor.
  35. */
  36. function createInvocableActor(invokeDefinition, machine, context, _event) {
  37. var _a;
  38. var invokeSrc = toInvokeSource(invokeDefinition.src);
  39. var serviceCreator = (_a = machine === null || machine === void 0 ? void 0 : machine.options.services) === null || _a === void 0 ? void 0 : _a[invokeSrc.type];
  40. var resolvedData = invokeDefinition.data ? mapContext(invokeDefinition.data, context, _event) : undefined;
  41. var tempActor = serviceCreator ? createDeferredActor(serviceCreator, invokeDefinition.id, resolvedData) : createNullActor(invokeDefinition.id); // @ts-ignore
  42. tempActor.meta = invokeDefinition;
  43. return tempActor;
  44. }
  45. function createDeferredActor(entity, id, data) {
  46. var tempActor = createNullActor(id); // @ts-ignore
  47. tempActor.deferred = true;
  48. if (isMachine(entity)) {
  49. // "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here
  50. var initialState_1 = tempActor.state = provide(undefined, function () {
  51. return (data ? entity.withContext(data) : entity).initialState;
  52. });
  53. tempActor.getSnapshot = function () {
  54. return initialState_1;
  55. };
  56. }
  57. return tempActor;
  58. }
  59. function isActor(item) {
  60. try {
  61. return typeof item.send === 'function';
  62. } catch (e) {
  63. return false;
  64. }
  65. }
  66. function isSpawnedActor(item) {
  67. return isActor(item) && 'id' in item;
  68. } // TODO: refactor the return type, this could be written in a better way but it's best to avoid unneccessary breaking changes now
  69. function toActorRef(actorRefLike) {
  70. var _a;
  71. return __assign((_a = {
  72. subscribe: function () {
  73. return {
  74. unsubscribe: function () {
  75. return void 0;
  76. }
  77. };
  78. },
  79. id: 'anonymous',
  80. getSnapshot: function () {
  81. return undefined;
  82. }
  83. }, _a[symbolObservable] = function () {
  84. return this;
  85. }, _a), actorRefLike);
  86. }
  87. export { createDeferredActor, createInvocableActor, createNullActor, isActor, isSpawnedActor, toActorRef };