Skip to content

Commit

Permalink
update _azleIc type to be possibly undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 18, 2023
1 parent 97b2048 commit 56ebb48
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AzleIc } from './ic/types/azle_ic';
import { Buffer } from 'buffer';

declare global {
var _azleIc: AzleIc;
var _azleIc: AzleIc | undefined;
var _azleResolveIds: { [key: string]: (buf: ArrayBuffer) => void };
var _azleRejectIds: { [key: string]: (err: any) => void };
var _azleIcTimers: { [key: string]: string };
Expand All @@ -13,7 +13,7 @@ declare global {

globalThis.TextDecoder = require('text-encoding').TextDecoder;
globalThis.TextEncoder = require('text-encoding').TextEncoder;
globalThis._azleIcTimers ||= {};
globalThis._azleIcTimers = {};
globalThis._azleResolveIds = {};
globalThis._azleRejectIds = {};
globalThis._azleTimerCallbackIds = {};
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ic/call_raw_128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function callRaw128(

// TODO this should use a Result remember
return new Promise((resolve, reject) => {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const promiseId = v4();
const globalResolveId = `_resolve_${promiseId}`;
const globalRejectId = `_reject_${promiseId}`;
Expand Down
36 changes: 36 additions & 0 deletions src/lib/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function StableBTreeMap<
* @returns `true` if the key exists in the map, `false` otherwise.
*/
containsKey(key: TypeMapping<Key>): boolean {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

Expand All @@ -36,6 +40,10 @@ export function StableBTreeMap<
* @returns the value associated with the given key, if it exists.
*/
get(key: TypeMapping<Key>): Opt<TypeMapping<Value>> {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

Expand All @@ -60,6 +68,10 @@ export function StableBTreeMap<
key: TypeMapping<Key>,
value: TypeMapping<Value>
): Opt<TypeMapping<Value>> {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;
const candidEncodedValue = encode(valueType, value).buffer;
Expand All @@ -82,6 +94,10 @@ export function StableBTreeMap<
* @returns `true` if the map contains no elements, `false` otherwise.
*/
isEmpty(): boolean {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

return globalThis._azleIc.stableBTreeMapIsEmpty(
Expand All @@ -93,6 +109,10 @@ export function StableBTreeMap<
* @returns tuples representing key/value pairs.
*/
items(): [TypeMapping<Key>, TypeMapping<Value>][] {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedItems = globalThis._azleIc.stableBTreeMapItems(
Expand All @@ -112,6 +132,10 @@ export function StableBTreeMap<
* @returns they keys in the map.
*/
keys(): TypeMapping<Key>[] {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedKeys = globalThis._azleIc.stableBTreeMapKeys(
Expand All @@ -128,6 +152,10 @@ export function StableBTreeMap<
* @returns the number of elements in the map.
*/
len(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedLen = globalThis._azleIc.stableBTreeMapLen(
Expand All @@ -142,6 +170,10 @@ export function StableBTreeMap<
* @returns the previous value at the key if it exists, `null` otherwise.
*/
remove(key: TypeMapping<Key>): Opt<TypeMapping<Value>> {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

Expand All @@ -161,6 +193,10 @@ export function StableBTreeMap<
* @returns the values in the map.
*/
values(): TypeMapping<Value>[] {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedValues = globalThis._azleIc.stableBTreeMapValues(
Expand Down

0 comments on commit 56ebb48

Please sign in to comment.