Snack SDK Migration Guide

December 4, 2020 ยท View on GitHub

This guide helps with migration from snack-sdk@2 to snack-sdk@3.

Conceptually, v3 is similar to v2 and exposes a Snack Session class which can be used to create Snacks, save them and connect to them using the Expo client. The API and types have however been overhauled to provide a leaner and more consistent API. V3 also supports first class TypeScript support, but the flow typings have been removed.

Imports

Before (using flow)

import { SnackSession } from 'snack-sdk'; // 2.x.x
import type { ExpoSnackFiles } from 'snack-sdk'; // 2.x.x

const files: ExpoSnackFiles = {
  'App.js': {
    type: 'CODE',
    contents: `console.log('Hello Snack!');`,
  },
};
const session = new SnackSession({
  ...
});
session.sendCodeAsync(files);

After (using TypeScript)

import { Snack, SnackFiles } from 'snack-sdk'; // 3.x.x

const files: SnackFiles = {
  'App.js': {
    type: 'CODE',
    contents: `console.log('Hello Snack!');`,
  },
};
const snack = new Snack({
  files,
});

Snack class comparison

Version 2Version 3Description
startAsyncMethod was removed, use setOnline(true) instead.
stopAsyncMethod was removed, use setOnline(false) instead.
setPubNubEnabledMethod was removed, use setOnline instead.
getUrlAsyncMethod has been removed, use getState().url instead.
getChannelMethod was removed, use getState().channel instead.
setOnlinestartAsync and setPubNubEnabled were tightly coupled and could result in invalid behavior when used incorrectly. These APIs have been merged into the setOnline method which starts both the PubNub transport and the DevSession advertisement as the same time.
sendCodeAsyncMethod was removed, use updateFiles instead.
updateFilesUpdates the code/asset files after which they are automatically sent to the connected clients whenever ready. This method replaces sendCodeAsync.
addModuleAsyncMethod was removed, use updateDependencies instead.
removeModuleAsyncMethod was removed, use updateDependencies instead.
syncDependenciesAsyncMethod was removed, use updateDependencies instead.
updateDependenciesUpdates the dependencies and starts the resolving process if needed. This method replaces addModuleAsync, removeModuleAsync and syncDependenciesAsync.
reloadSnackreloadConnectedClientsMethod was renamed to reloadConnectedClients. In contrast to reloadSnack, reloadConnectedClients waits for all clients to be reloaded before resolving the returned Promise.
setSdkVersionsetSDKVersionMethod was renamed to setSDKVersion.
setFocus(true/false)setFocus()Method signature has changed and the boolean argument was removed. It is now only possible to indicate that the Snack has received the focus.
saveAsyncsaveAsyncNo changes.
downloadAsyncgetDownloadURLAsyncMethod was renamed to getDownloadURLAsync and allows options for saveAsync to be passed to it.
uploadAssetAsyncuploadAssetAsyncNo changes.
setNamesetNameNo changes.
setDescriptionsetDescriptionNo changes.
setDeviceIdsetDeviceIdNo changes.
setUsersetUserNo changes.
setSessionSecret (deprecated)Method was removed, use setUser instead.
setAuthorizationToken (deprecated)Method was removed, use setUser instead.
getStategetStateReturns the new SnackState object.
getStateAsyncWaits for any async operations such as asset uploads and dependency resolvers to complete before returning the state.
addStateListeneraddStateListenerMethod signature was changed.
addLogListeneraddLogListenerMethod signature was changed.
addErrorListenerMethod was removed, use addStateListener instead and compare whether the errors have changed.
addPresenceListenerMethod was removed, use addStateListener instead and compare the connectedClients field.
supportsFeatureMethod was removed.
getPreviewAsyncNew method that requests a preview from the connected devices and returns an image URL.
setCodeChangesDelayNew method that sets the wait time before notifying the connected clients of any code updates.
sendCodeChangesNew method that triggers an immediate send of any pending code changes to the connected clients.
setDisabledNew method that disables the Snack entirely, effectively disabling any asynchronous asset uploads and dependency resolutions.

Globals comparison

Version 2Version 3Description
SDKVersions.versiongetSupportedSDKVersionsUpdated to method called getSupportedSDKVersions.
SDKVersions.sdkSupportsFeatureisFeatureSupportedUpdated to method called isFeatureSupported.
isModulePreloadedisModulePreloadedHas been extended with optional 3rd parameter coreModulesOnly.
preloadedModulesgetPreloadedModulesUpdated to method called getPreloadedModules.
dependencyUtilsstandardizeDependenciesReplaced by method standardizeDependencies.
supportedModulesField has been removed.
getSupportedVersionMethod has been removed.
isValidSemverChecks whether a string is a valid semantic version.

Types

Nearly all type names have changed. Most notably, the Expo prefix has been removed from the types and all types now start with Snack.

The formats for the files and dependencies has remained the same compared to v2. The V1 compact files format has however been dropped and all files should be explicitely listed in the SnackFiles collection.