@ic-reactor/core 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,6 +16,12 @@ npm install @ic-reactor/core
16
16
  yarn add @ic-reactor/core
17
17
  ```
18
18
 
19
+ or you can use the UMD version:
20
+
21
+ ```html
22
+ <script src="https://github.com/B3Pay/ic-reactor/releases/download/1.0.8/ic-reactor-core.min.js"></script>
23
+ ```
24
+
19
25
  ### Using `createReactorCore`
20
26
 
21
27
  For ease of use, the `createReactorCore` factory function automatically sets up a new Reactor instance, managing the agent and its state internally, and providing a simple API for authenticating, querying, and updating actors.
@@ -5,6 +5,8 @@ export declare class ActorManager<A = BaseActor> {
5
5
  private _actor;
6
6
  private _idlFactory;
7
7
  private _agentManager;
8
+ private _unsubscribeAgent;
9
+ private _subscribers;
8
10
  canisterId: CanisterId;
9
11
  actorStore: ActorStore<A>;
10
12
  visitFunction: VisitService<A>;
@@ -25,4 +27,5 @@ export declare class ActorManager<A = BaseActor> {
25
27
  getState: ActorStore<A>["getState"];
26
28
  subscribeActorState: ActorStore<A>["subscribe"];
27
29
  setState: ActorStore<A>["setState"];
30
+ cleanup: () => void;
28
31
  }
@@ -16,14 +16,15 @@ const helper_1 = require("../../utils/helper");
16
16
  class ActorManager {
17
17
  constructor(actorConfig) {
18
18
  this._actor = null;
19
+ this._subscribers = [];
19
20
  this.initialState = {
20
21
  methodState: {},
21
22
  initializing: false,
22
23
  initialized: false,
23
24
  error: undefined,
24
25
  };
25
- this.updateState = (newState) => {
26
- this.actorStore.setState((state) => (Object.assign(Object.assign({}, state), newState)));
26
+ this.updateState = (newState, action) => {
27
+ this.actorStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
27
28
  };
28
29
  this.updateMethodState = (method, hash, newState) => {
29
30
  this.actorStore.setState((state) => {
@@ -31,7 +32,7 @@ class ActorManager {
31
32
  const currentMethodState = methodState[hash] || DEFAULT_STATE;
32
33
  const updatedMethodState = Object.assign(Object.assign({}, methodState), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) });
33
34
  return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: updatedMethodState }) });
34
- });
35
+ }, false, method);
35
36
  };
36
37
  this.initialize = (options) => __awaiter(this, void 0, void 0, function* () {
37
38
  yield this._agentManager.updateAgent(options);
@@ -52,7 +53,7 @@ class ActorManager {
52
53
  };
53
54
  this.initializeActor = (agent) => {
54
55
  console.info(`Initializing actor ${this.canisterId} on ${agent.isLocal() ? "local" : "ic"} network`);
55
- const { _idlFactory: idlFactory, canisterId } = this;
56
+ const { _idlFactory, canisterId } = this;
56
57
  this.updateState({
57
58
  initializing: true,
58
59
  initialized: false,
@@ -62,7 +63,7 @@ class ActorManager {
62
63
  if (!agent) {
63
64
  throw new Error("Agent not initialized");
64
65
  }
65
- this._actor = agent_1.Actor.createActor(idlFactory, {
66
+ this._actor = agent_1.Actor.createActor(_idlFactory, {
66
67
  agent,
67
68
  canisterId,
68
69
  });
@@ -99,11 +100,17 @@ class ActorManager {
99
100
  return this.actorStore.getState();
100
101
  };
101
102
  this.subscribeActorState = (listener) => {
102
- return this.actorStore.subscribe(listener);
103
+ const unsubscribe = this.actorStore.subscribe(listener);
104
+ this._subscribers.push(unsubscribe);
105
+ return unsubscribe;
103
106
  };
104
107
  this.setState = (updater) => {
105
108
  return this.actorStore.setState(updater);
106
109
  };
110
+ this.cleanup = () => {
111
+ this._unsubscribeAgent();
112
+ this._subscribers.forEach((unsubscribe) => unsubscribe());
113
+ };
107
114
  const { agentManager, canisterId, idlFactory, withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
108
115
  this.canisterId = canisterId;
109
116
  this._idlFactory = idlFactory;
@@ -113,7 +120,7 @@ class ActorManager {
113
120
  withDevtools,
114
121
  store: `actor-${String(canisterId)}`,
115
122
  });
116
- this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
123
+ this._unsubscribeAgent = this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
117
124
  if (withVisitor) {
118
125
  this.visitFunction = this.extractService();
119
126
  }
@@ -1,7 +1,7 @@
1
1
  import type { IDL } from "@dfinity/candid";
2
- import type { StoreApi } from "zustand";
3
2
  import type { AgentManager } from "../agent";
4
3
  import type { ActorMethod, ActorSubclass, Principal } from "../../types";
4
+ import { StoreApiWithDevtools } from "../types";
5
5
  export interface DefaultActorType {
6
6
  [key: string]: ActorMethod;
7
7
  }
@@ -42,5 +42,5 @@ export type ActorState<A = BaseActor> = {
42
42
  error: Error | undefined;
43
43
  methodState: ActorMethodStates<A>;
44
44
  };
45
- export type ActorStore<A = BaseActor> = StoreApi<ActorState<A>>;
45
+ export type ActorStore<A = BaseActor> = StoreApiWithDevtools<ActorState<A>>;
46
46
  export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
@@ -1,5 +1,7 @@
1
1
  import { HttpAgent } from "@dfinity/agent";
2
- import type { AgentStore, AgentManagerParameters, UpdateAgentParameters, AuthStore, AuthClient } from "./types";
2
+ import { AuthClient } from "@dfinity/auth-client";
3
+ import type { AuthClientLoginOptions } from "../../types";
4
+ import type { AgentStore, AgentManagerParameters, UpdateAgentParameters, AuthStore } from "./types";
3
5
  export declare class AgentManager {
4
6
  private _agent;
5
7
  private _auth;
@@ -18,6 +20,10 @@ export declare class AgentManager {
18
20
  private notifySubscribers;
19
21
  updateAgent: (options?: UpdateAgentParameters) => Promise<void>;
20
22
  authenticate: () => Promise<import("@dfinity/agent").Identity>;
23
+ login: (options?: AuthClientLoginOptions) => Promise<void>;
24
+ logout: (options?: {
25
+ returnTo?: string;
26
+ }) => Promise<void>;
21
27
  getAgent: () => HttpAgent;
22
28
  getAgentState: AgentStore["getState"];
23
29
  subscribeAgentState: AgentStore["subscribe"];
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,6 +23,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
46
23
  exports.AgentManager = void 0;
47
24
  const agent_1 = require("@dfinity/agent");
48
25
  const helper_1 = require("../../utils/helper");
26
+ const auth_client_1 = require("@dfinity/auth-client");
49
27
  const constants_1 = require("../../utils/constants");
50
28
  class AgentManager {
51
29
  constructor(options) {
@@ -62,11 +40,11 @@ class AgentManager {
62
40
  authenticated: false,
63
41
  error: undefined,
64
42
  };
65
- this.updateAgentState = (newState) => {
66
- this.agentStore.setState((state) => (Object.assign(Object.assign({}, state), newState)));
43
+ this.updateAgentState = (newState, action) => {
44
+ this.agentStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
67
45
  };
68
- this.updateAuthState = (newState) => {
69
- this.authStore.setState((state) => (Object.assign(Object.assign({}, state), newState)));
46
+ this.updateAuthState = (newState, action) => {
47
+ this.authStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
70
48
  };
71
49
  this.initializeAgent = () => __awaiter(this, void 0, void 0, function* () {
72
50
  this.updateAgentState({ initializing: true });
@@ -108,12 +86,7 @@ class AgentManager {
108
86
  this.authenticate = () => __awaiter(this, void 0, void 0, function* () {
109
87
  this.updateAuthState({ authenticating: true });
110
88
  try {
111
- const { AuthClient } = yield Promise.resolve().then(() => __importStar(require("@dfinity/auth-client"))).catch((error) => {
112
- // eslint-disable-next-line no-console
113
- console.error("Failed to import @dfinity/auth-client:", error);
114
- throw new Error("Authentication failed: @dfinity/auth-client package is missing.");
115
- });
116
- this._auth = yield AuthClient.create();
89
+ this._auth = yield auth_client_1.AuthClient.create();
117
90
  const authenticated = yield this._auth.isAuthenticated();
118
91
  const identity = this._auth.getIdentity();
119
92
  this._agent.replaceIdentity(identity);
@@ -130,6 +103,29 @@ class AgentManager {
130
103
  throw error;
131
104
  }
132
105
  });
106
+ this.login = (options) => __awaiter(this, void 0, void 0, function* () {
107
+ this.updateAuthState({ authenticating: true });
108
+ if (!this._auth) {
109
+ yield this.authenticate();
110
+ }
111
+ if (!this._auth) {
112
+ throw new Error("Auth client not initialized");
113
+ }
114
+ yield this._auth.login(Object.assign(Object.assign({ identityProvider: this.isLocalEnv
115
+ ? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
116
+ : constants_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => __awaiter(this, void 0, void 0, function* () {
117
+ var _b;
118
+ yield this.authenticate();
119
+ (_b = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _b === void 0 ? void 0 : _b.call(options);
120
+ }) }));
121
+ });
122
+ this.logout = (options) => __awaiter(this, void 0, void 0, function* () {
123
+ if (!this._auth) {
124
+ throw new Error("Auth client not initialized");
125
+ }
126
+ yield this._auth.logout(options);
127
+ yield this.authenticate();
128
+ });
133
129
  // agent store
134
130
  this.getAgent = () => {
135
131
  return this._agent;
@@ -1,6 +1,6 @@
1
1
  import type { HttpAgent, HttpAgentOptions, Identity } from "@dfinity/agent";
2
2
  import type { AuthClient } from "@dfinity/auth-client";
3
- import type { StoreApi } from "zustand";
3
+ import { StoreApiWithDevtools } from "../types";
4
4
  export { HttpAgentOptions, AuthClient, Identity };
5
5
  export interface AgentManagerParameters extends HttpAgentOptions {
6
6
  port?: number;
@@ -21,5 +21,5 @@ export interface AuthState {
21
21
  export interface UpdateAgentParameters extends HttpAgentOptions {
22
22
  agent?: HttpAgent;
23
23
  }
24
- export type AgentStore = StoreApi<AgentState>;
25
- export type AuthStore = StoreApi<AuthState>;
24
+ export type AgentStore = StoreApiWithDevtools<AgentState>;
25
+ export type AuthStore = StoreApiWithDevtools<AuthState>;
@@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CandidAdapter = void 0;
13
13
  const agent_1 = require("@dfinity/agent");
14
- const principal_1 = require("@dfinity/principal");
15
14
  const constants_1 = require("../../utils/constants");
16
15
  class CandidAdapter {
17
16
  constructor({ agentManager, agent, didjsCanisterId, }) {
@@ -49,12 +48,9 @@ class CandidAdapter {
49
48
  }
50
49
  getFromMetadata(canisterId) {
51
50
  return __awaiter(this, void 0, void 0, function* () {
52
- if (typeof canisterId === "string") {
53
- canisterId = principal_1.Principal.fromText(canisterId);
54
- }
55
51
  const status = yield agent_1.CanisterStatus.request({
56
52
  agent: this.agent,
57
- canisterId,
53
+ canisterId: canisterId,
58
54
  paths: ["candid"],
59
55
  });
60
56
  const did = status.get("candid");
@@ -0,0 +1,8 @@
1
+ export * from "./agent/types";
2
+ export * from "./actor/types";
3
+ export * from "./candid/types";
4
+ import { NamedSet } from "zustand/middleware";
5
+ import type { StoreApi } from "zustand";
6
+ export interface StoreApiWithDevtools<T> extends StoreApi<T> {
7
+ setState: NamedSet<T>;
8
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./agent/types"), exports);
18
+ __exportStar(require("./actor/types"), exports);
19
+ __exportStar(require("./candid/types"), exports);
@@ -21,7 +21,6 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.createReactorCore = void 0;
24
- const constants_1 = require("./utils/constants");
25
24
  const utils_1 = require("./utils");
26
25
  const createReactorStore_1 = require("./createReactorStore");
27
26
  /**
@@ -114,32 +113,10 @@ const createReactorCore = (config) => {
114
113
  const updateCall = ({ functionName, args = [] }) => {
115
114
  return actorMethod(functionName, ...args);
116
115
  };
117
- const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
118
- const authClient = agentManager.getAuth();
119
- if (!authClient) {
120
- yield agentManager.authenticate();
121
- }
122
- if (!authClient) {
123
- throw new Error("Auth client not initialized");
124
- }
125
- yield authClient.login(Object.assign({ identityProvider: agentManager.isLocalEnv
126
- ? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
127
- : constants_1.IC_INTERNET_IDENTITY_PROVIDER }, options));
128
- });
129
- const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
130
- const authClient = agentManager.getAuth();
131
- if (!authClient) {
132
- throw new Error("Auth client not initialized");
133
- }
134
- yield authClient.logout(options);
135
- yield agentManager.authenticate();
136
- });
137
116
  return Object.assign(Object.assign({ queryCall,
138
117
  updateCall,
139
118
  callMethod,
140
119
  getState,
141
- login,
142
- logout,
143
120
  subscribeActorState }, agentManager), rest);
144
121
  };
145
122
  exports.createReactorCore = createReactorCore;
package/dist/types.d.ts CHANGED
@@ -6,9 +6,7 @@ import type { ActorManagerParameters, ActorMethodParameters, ActorMethodReturnTy
6
6
  import type { ActorManager } from "./classes/actor";
7
7
  import type { AgentManager } from "./classes/agent";
8
8
  import type { AuthClientLoginOptions } from "@dfinity/auth-client";
9
- export * from "./classes/agent/types";
10
- export * from "./classes/actor/types";
11
- export * from "./classes/candid/types";
9
+ export * from "./classes/types";
12
10
  export type { ActorManager, AgentManager };
13
11
  export type { ActorMethod, AuthClientLoginOptions, HttpAgentOptions, ActorSubclass, Principal, HttpAgent, Identity, IDL, };
14
12
  export interface CreateReactorStoreParameters extends HttpAgentOptions, Omit<ActorManagerParameters, "agentManager"> {
@@ -56,10 +54,6 @@ export interface CreateReactorCoreParameters extends CreateReactorStoreParameter
56
54
  withProcessEnv?: boolean;
57
55
  }
58
56
  export interface CreateReactorCoreReturnType<A = BaseActor> extends AgentManager, Omit<ActorManager<A>, "updateMethodState"> {
59
- login: (config?: AuthClientLoginOptions) => Promise<void>;
60
- logout: (config?: {
61
- returnTo?: string;
62
- }) => Promise<void>;
63
57
  queryCall: ActorQuery<A>;
64
58
  updateCall: ActorUpdate<A>;
65
59
  }
package/dist/types.js CHANGED
@@ -14,6 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./classes/agent/types"), exports);
18
- __exportStar(require("./classes/actor/types"), exports);
19
- __exportStar(require("./classes/candid/types"), exports);
17
+ __exportStar(require("./classes/types"), exports);
@@ -9,6 +9,9 @@ function createStoreWithOptionalDevtools(initialState, config) {
9
9
  return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, {
10
10
  name: "Reactor",
11
11
  store: config.store,
12
+ serialize: {
13
+ replacer: (_, value) => typeof value === "bigint" ? value.toString() : value,
14
+ },
12
15
  }));
13
16
  }
14
17
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "A library for intracting with the Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,11 +37,14 @@
37
37
  "scripts": {
38
38
  "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
39
39
  "start": "tsc watch",
40
- "build": "npx tsc",
40
+ "bundle": "npm run bundle:dev && npm run bundle:prod",
41
+ "bundle:dev": "npx webpack --mode development",
42
+ "bundle:prod": "npx webpack --mode production",
43
+ "build": "npx tsc && npm run bundle",
41
44
  "clean": "npx rimraf dist"
42
45
  },
43
46
  "engines": {
44
47
  "node": ">=10"
45
48
  },
46
- "gitHead": "ed812d516651ec01414d9a1a79e959d8cc139a93"
49
+ "gitHead": "4138171fa438e7db3764c0692e56f71e4bdd452c"
47
50
  }